package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.server.interceptor.UserHolder;

import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;

import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommentService {
    @Reference
    private CommentApi commentApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Autowired
    private StringRedisTemplate redisTemplate;

    public PageResult<CommentVo> findPage(Long page, Long pagesize, String movementId) {
        PageResult pageResult = commentApi.findPage(page, pagesize, movementId);
        List<Comment> items = pageResult.getItems();
        List<CommentVo> commentList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(items)) {
            List<Long> userIdList = items.stream().map(Comment::getUserId).collect(Collectors.toList());
            List<UserInfo> userinfoList = userInfoApi.findByBatchIds(userIdList);
            Map<Long, UserInfo> userInfoMap = userinfoList.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
            items.stream().map(comment -> {
                CommentVo vo = new CommentVo();
                BeanUtils.copyProperties(comment, vo);
                UserInfo userInfo = userInfoMap.get(comment.getUserId());
                BeanUtils.copyProperties(userInfo, vo);
                vo.setCreateDate(String.valueOf(comment.getCreated()));
                vo.setId(String.valueOf(comment.getTargetUserId()));
                String key = "publish_comment_" + UserHolder.getUserId() + "_" + comment.getId();
                if (redisTemplate.hasKey(key)) {
                    vo.setHasLiked(1);
                }
                commentList.add(vo);
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setItems(commentList);
        return pageResult;
    }

    public void saveComment(Map<String, String> param) {
        Long loninUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setUserId(loninUserId);
        comment.setCommentType(2);
        comment.setTargetType(2);
        comment.setContent(param.get("comment"));
        comment.setUserId(loninUserId);
        comment.setTargetId(new ObjectId(param.get("movementId")));
        commentApi.save(comment);
    }

    public Integer like(String commentId) {
        Long loginUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setTargetId(new ObjectId(commentId));
        comment.setCommentType(1);
        comment.setTargetType(1);
        comment.setUserId(loginUserId);
        Integer likeCount= commentApi.like(comment);
        String key = "publish_like_" + loginUserId + "_" + commentId;
        redisTemplate.opsForValue().set(key,"1");
        return likeCount;
    }


    public Integer dislike(String commentId) {
        Long loginUserId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setTargetId(new ObjectId(commentId));
        comment.setCommentType(1);
        comment.setUserId(loginUserId);
        Integer likeCount= commentApi.remove(comment);
        String key = "publish_like_" + loginUserId + "_" + commentId;
        redisTemplate.delete(key);
        return likeCount;
    }
}
